package com.zl.smartmall.library.im.po;

import java.util.ArrayList;
import java.util.List;

import net.tsz.afinal.annotation.sqlite.Property;
import net.tsz.afinal.annotation.sqlite.Table;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * 聊天界面消息类
 * @author 陈希然
 *
 */
@Table(name = "tb_message_info")
public class MessageInfo extends BaseMessageInfo{

	@Property
	private int lastId;
	@Property
	private int msgID;// 聊天消息id，在同一个chatID下，msgID是连续的，如果出现断层，表示中间可能丢消息了。
	@Property
	private int senderUid;// 本条消息的发信人的uid
	@Property
	private int receiverUid;// 本条消息的收信人的uid
	@Property
	private int msgType;// 消息类型，0=文字,1=图片,2=声音,3=视频,4=url,5=第三方分享
	@Property
	private int sendFrom;//哪方发送的消息
	@Property
	private int width;
	@Property 
	private int height;
	@Property
	private long relativeID;//在本地数据库标示两个人的聊天关系，两个人的UID拼在一起
	@Property
	private String msgPicture;
	@Property
	private String msgPictureThumb;
	@Property
	private String senderFace;
	@Property
	private String senderNickname;
	@Property
	private String receiverFace;
	@Property
	private String receiverNickname;

	public int getMsgID() {
		return msgID;
	}

	public void setMsgID(int msgID) {
		this.msgID = msgID;
	}

	public int getSenderUid() {
		return senderUid;
	}

	public void setSenderUid(int senderUid) {
		this.senderUid = senderUid;
	}

	public int getReceiverUid() {
		return receiverUid;
	}

	public void setReceiverUid(int receiverUid) {
		this.receiverUid = receiverUid;
	}

	public int getMsgType() {
		return msgType;
	}

	public void setMsgType(int msgType) {
		this.msgType = msgType;
	}

	public int getSendFrom() {
		return sendFrom;
	}

	public void setSendFrom(int sendFrom) {
		this.sendFrom = sendFrom;
	}

	public long getRelativeID() {
		return relativeID;
	}

	public void setRelativeID(long relativeID) {
		this.relativeID = relativeID;
	}

	public String getSenderFace() {
		return senderFace;
	}

	public void setSenderFace(String senderFace) {
		this.senderFace = senderFace;
	}

	public String getSenderNickname() {
		return senderNickname;
	}

	public void setSenderNickname(String senderNickname) {
		this.senderNickname = senderNickname;
	}

	public String getReceiverFace() {
		return receiverFace;
	}

	public void setReceiverFace(String receiverFace) {
		this.receiverFace = receiverFace;
	}

	public String getReceiverNickname() {
		return receiverNickname;
	}

	public void setReceiverNickname(String receiverNickname) {
		this.receiverNickname = receiverNickname;
	}

	public int getLastId() {
		return lastId;
	}

	public void setLastId(int lastId) {
		this.lastId = lastId;
	}

	public int getWidth() {
		return width;
	}

	public void setWidth(int width) {
		this.width = width;
	}

	public int getHeight() {
		return height;
	}

	public void setHeight(int height) {
		this.height = height;
	}

	public String getMsgPicture() {
		return msgPicture;
	}

	public void setMsgPicture(String msgPicture) {
		this.msgPicture = msgPicture;
	}

	public String getMsgPictureThumb() {
		return msgPictureThumb;
	}

	public void setMsgPictureThumb(String msgPictureThumb) {
		this.msgPictureThumb = msgPictureThumb;
	}

	private static MessageInfo parse(JSONObject jsonObject, int msgFrom) throws JSONException {
		if(jsonObject != null && jsonObject.length() > 0) {
			MessageInfo info = new MessageInfo();
			info.chatId = jsonObject.getInt("pmid");
			info.height = jsonObject.getInt("height");
			info.lastId = jsonObject.getInt("lastid");
			info.message = jsonObject.getString("content");
			info.msgID = jsonObject.getInt("msgid");
			info.msgPicture = jsonObject.getString("picture");
			info.msgPictureThumb = jsonObject.getString("picture_thumb");
			info.msgType = jsonObject.getInt("type");
			info.receiverUid = jsonObject.getInt("fuid");
			info.senderUid = jsonObject.getInt("uid");
			info.time = jsonObject.getLong("dateline");
			info.width = jsonObject.getInt("width");
			info.sendFrom = msgFrom;
			return info;
		}
		
		return null;
	}
	
	public static List<MessageInfo> parse(JSONArray jsonArray, int msgFrom) throws JSONException {
		List<MessageInfo> messageInfos = new ArrayList<MessageInfo>();
		for(int i = 0; jsonArray != null && i < jsonArray.length(); i++) {
			MessageInfo messageInfo = parse(jsonArray.getJSONObject(i), msgFrom);
			if(messageInfo != null) {
				messageInfos.add(messageInfo);
			}
		}
		return messageInfos;
	}
}
